import Dayjs from 'dayjs';
import {
  GetHoliday,
  GetPerpetualCalendarMonth
} from '@/apis/attendance/base-setting/index';

export default function calender(type: number) {
  const nowDate = Dayjs('2023-11-1').startOf('d');
  const dayList = reactive<any>({ data: [] });
  const showMonth = ref(nowDate);
  const isWeek = ref();
  const weekNum = ref();
  const perpetualCalendarData: any = reactive({ data: [] });

  const showMonthChnage = (e: any, mode?: string) => {
    if (typeof e === 'number') {
      const number = showMonth.value.month() + 1 - e;
      showMonth.value = showMonth.value.subtract(number, 'M');
    } else {
      if (
        (showMonth.value.format('M') === '12' && mode === 'next') ||
        (showMonth.value.format('M') === '1' && mode === 'last')
      ) {
        return;
      }
      showMonth.value =
        mode === 'last'
          ? showMonth.value.subtract(1, 'M')
          : showMonth.value.add(1, 'M');
    }
    initList();
  };

  const showWeekChnage = (mode: number) => {
    const offset = mode - isWeek.value;
    showMonth.value = showMonth.value.add(offset, 'w');
    isWeek.value = mode;
    initList();
  };

  const nowWeek = () => {
    const today = new Date(showMonth.value.format('YYYY-MM-DD'));
    const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
    const dayOfWeek = firstDayOfMonth.getDay();
    const dayOfMonth = today.getDate();
    const weekOfMonth = Math.ceil((dayOfMonth + dayOfWeek) / 7);
    const lastDayOfMonth = new Date(
      today.getFullYear(),
      today.getMonth() + 1,
      0
    );
    const daysInMonth = lastDayOfMonth.getDate();
    const daysInFirstWeek = 7 - dayOfWeek;
    const daysInRemainingWeeks = daysInMonth - daysInFirstWeek;
    const weeksInMonth = 1 + Math.ceil(daysInRemainingWeeks / 7);
    isWeek.value = weekOfMonth;
    weekNum.value = weeksInMonth;
    console.log('isWeek.value ', isWeek.value, weekNum.value, today);
  };
  nowWeek();
  const isPerpetualCalendar = (date: any) => {
    const month = date.month() + 1;
    const day = date.format('YYYY-MM-DD');
    const dayName = perpetualCalendarData.data
      .filter((ele: any) => ele.month === month)[0]
      ?.days.filter((ele: any) => ele.date === day)[0]?.lunarCalendar;
    return dayName;
  };

  const initList = () => {
    const monthStart = showMonth.value.startOf('M');
    const monthEnd = showMonth.value.endOf('M').startOf('D');
    const list: any = [];
    if (type === 3) {
      console.log('props.type ', type);
      const weekStart = showMonth.value.startOf('week').add(1, 'd');
      const weekEnd = showMonth.value.endOf('week').add(1, 'd').startOf('D');
      const month = showMonth.value.month();
      const weekStartMonth = weekStart.month();
      const weekEndMonth = weekEnd.month();
      let firstWeek = 0;
      const startNum: number =
        weekStart.toDate().getDate() +
        (monthStart.day() ? monthStart.day() - 1 : 6) -
        1;
      if (month === weekStartMonth) {
        // startNum =
        //   weekStart.toDate().getDate() +
        //   (monthStart.day() ? monthStart.day() - 1 : 6) -
        //   1;
      } else {
        firstWeek =
          weekStart.endOf('M').startOf('D').day() - weekStart.day() + 1;
        console.log(
          234,
          weekStart,
          weekStart.endOf('M').startOf('D'),
          weekStartMonth,
          weekStart.endOf('M').startOf('D').day()
        );
      }
      for (let i = firstWeek ? firstWeek : startNum; i > 0; i--) {
        const dateJs = !firstWeek
          ? weekStart.subtract(
              (firstWeek ? firstWeek : startNum) - i + 1,
              'day'
            )
          : monthStart.subtract(firstWeek - i + 1, 'd');
        list.splice(0, 0, {
          dateJs,
          millisecond: dateJs.valueOf(),
          type: 'lastMonth',
          perpetualCalendar: isPerpetualCalendar(dateJs)
        });
      }
      const centerNum = 7 - firstWeek;
      for (let i = 0; i < centerNum; i += 1) {
        const dateJs = weekStart.add(i, 'd');
        list.push({
          dateJs,
          millisecond: dateJs.valueOf(),
          type: 'thisMonth',
          perpetualCalendar: isPerpetualCalendar(dateJs)
        });
      }
      const endNum =
        monthEnd.toDate().getDate() -
        weekEnd.toDate().getDate() +
        (7 - (monthEnd.day() || 7));
      for (let i = 0; i < endNum; i += 1) {
        const dateJs = weekEnd.add(i, 'd');
        list.push({
          dateJs,
          millisecond: dateJs.valueOf(),
          type: 'nextMonth',
          perpetualCalendar: isPerpetualCalendar(dateJs)
        });
      }
    } else {
      const startNum = monthStart.day() ? monthStart.day() - 1 : 6;
      for (let i = 0; i < startNum; i += 1) {
        const dateJs = monthStart.subtract(i + 1, 'd');
        console.log(1, i, startNum, dateJs);
        list.splice(0, 0, {
          dateJs,
          millisecond: dateJs.valueOf(),
          type: 'lastMonth',
          perpetualCalendar: isPerpetualCalendar(dateJs)
        });
      }
      const centerNum = monthEnd.toDate().getDate();
      for (let i = 0; i < centerNum; i += 1) {
        const dateJs = monthStart.add(i, 'd');
        list.push({
          dateJs,
          millisecond: dateJs.valueOf(),
          type: 'thisMonth',
          perpetualCalendar: isPerpetualCalendar(dateJs)
        });
      }
      const endNum = 7 - (monthEnd.day() || 7);
      for (let i = 0; i < endNum; i += 1) {
        const dateJs = monthEnd.add(i + 1, 'd');
        list.push({
          dateJs,
          millisecond: dateJs.valueOf(),
          type: 'nextMonth',
          perpetualCalendar: isPerpetualCalendar(dateJs)
        });
      }
    }
    dayList.data = list;
  };
  const init = async () => {
    const res = await GetHoliday(2023);
    // const res2 = await GetPerpetualCalendarMonth(202311, true);
    Object.assign(perpetualCalendarData.data, res.data);
    initList();
  };
  return {
    showMonthChnage,
    showWeekChnage,
    nowWeek,
    showMonth: showMonth.value,
    isWeek: isWeek.value,
    weekNum: weekNum.value
  };
}
